<div class="form">
    <?php echo validation_errors(); ?>
    
    <?php echo form_open($action); ?>
        <div id="tabs">
            <ul>
                <li><a href="#tabs-1">基本信息</a></li>
                <li><a href="#tabs-2">费用统计</a></li>
                <li><a href="#tabs-3"><?php echo $dynamic_title; ?></a></li>
                <li><a href="#tabs-4">时限提醒</a></li>
                <li><a href="#tabs-5">官方文件</a></li>
            </ul>
            <div id="tabs-1">
                <div>
                    <label class="span-4">档案号<span class="required">*</span></label>
                    <input class="text span-4" id="file_no" name="file_no" readonly="readonly" 
                           value="<?php echo set_value('file_no', $trademark['file_no']); ?>" />
                    <label class="span-3">建档日期<span class="required">*</span></label>
                    <input class="text span-4 normal-date" name="file_create_date" readonly="readonly"
                           value="<?php echo set_value('file_create_date', $trademark['file_create_date']); ?>" />
                    <label class="span-3">案件类型<span class="required">*</span></label>
                    <?php echo form_dropdown('case_type', $case_types, set_value('case_type', $trademark['case_type']), 'class="span-4" id="case_type"'); ?>
                    <div class="clear"></div>
                </div>
                <hr class="space">
                <div>
                    <label class="span-4">申请人名称（中文）<span class="required">*</span></label>
                    <input class="text span-17" id="c_name" name="c_name" readonly="readonly"
                           value="<?php echo set_value('c_name', $trademark['c_name']); ?>" />
                    <button type="button" class="adhere" id="set_c_name">...</button>
                    <?php echo form_dropdown('c_id', $client_options, '', 'class="chosen-select span-18" id="c_id"'); ?>
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">（英文）</label>
                    <input class="text span-18" id="c_name_en" name="c_name_en" readonly="readonly"
                           value="<?php echo set_value('c_name_en', $trademark['c_name_en']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">申请人地址（中文）</label>
                    <input class="text span-18" id="c_addr" name="c_addr" readonly="readonly"
                           value="<?php echo set_value('c_addr', $trademark['c_addr']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">（英文）</label>
                    <input class="text span-18" id="c_addr_en" name="c_addr_en" readonly="readonly" 
                           value="<?php echo set_value('c_addr_en', $trademark['c_addr_en']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">证照编号</label>
                    <input class="text span-11" id="c_licence_number" name="c_licence_number"  readonly="readonly"
                           value="<?php echo set_value('c_licence_number', $trademark['c_licence_number']); ?>" />
                    <label class="span-3">国家</label>
                    <input class="text span-4" id="c_country" name="c_country"  readonly="readonly"
                           value="<?php echo set_value('c_country', $trademark['c_country']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">其他申请人信息</label>
                    <input class="text span-18" name="other_client_info"
                           value="<?php echo set_value('other_client_info', $trademark['other_client_info']); ?>" />
                    <div class="clear"></div>
                </div>
                <hr class="space">
                <div>
                    <label class="span-4">代理公司<span class="required">*</span></label>
                    <?php echo form_dropdown('agency', $agencies, set_value('agency', $trademark['agency']), 'class="span-11"'); ?>
                    
                    <label class="span-3">代理人<span class="required">*</span></label>
                    <input class="text span-3" id="agent" name="agent" readonly="readonly"
                           value="<?php echo set_value('agent', $trademark['agent']); ?>" />
                    <button type="button" class="adhere" id="set_agent">...</button>
                    <?php echo form_dropdown('agent_id', $user_options, '', 'class="span-4 hide" id="agent_id"'); ?>
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">合作所</label>
                    <input class="text span-10" id="partner" name="partner" readonly="readonly"
                           value="<?php echo set_value('partner', $trademark['partner']); ?>" />
                    <button type="button" class="adhere" id="set_partner">...</button>
                    <?php echo form_dropdown('partner_id', $partner_options, '', 'class="span-11 hide" id="partner_id"'); ?>
                    <label class="span-3">联系人<span class="required">*</span></label>
                    <input class="text span-4" id="ct_contact" name="ct_contact"
                           value="<?php echo set_value('ct_contact', $trademark['ct_contact']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">电话<span class="required">*</span></label>
                    <input class="text span-4" id="ct_telephone" name="ct_telephone"
                           value="<?php echo set_value('ct_telephone', $trademark['ct_telephone']); ?>" />
                    <label class="span-3">传真</label>
                    <input class="text span-4" id="ct_fax" name="ct_fax"
                           value="<?php echo set_value('ct_fax', $trademark['ct_fax']); ?>" />
                    <label class="span-3">邮箱</label>
                    <input class="text span-4" id="ct_email" name="ct_email"
                           value="<?php echo set_value('ct_email', $trademark['ct_email']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">邮寄地址<span class="required">*</span></label>
                    <input class="text span-11" id="ct_mailing_addr" name="ct_mailing_addr"
                           value="<?php echo set_value('ct_mailing_addr', $trademark['ct_mailing_addr']); ?>" />
                    <label class="span-3">QQ号</label>
                    <input class="text span-4" id="ct_qq" name="ct_qq"
                           value="<?php echo set_value('ct_qq', $trademark['ct_qq']); ?>" />
                    <div class="clear"></div>
                </div>
            </div>
            <div id="tabs-2">
                <div>
                    <label class="span-4">官费<span class="required">*</span></label>
                    <input class="text span-4" id="government_fee" name="government_fee" list="government_fees"
                           value="<?php echo set_value('government_fee', $trademark['government_fee']); ?>" />
                    <datalist id="government_fees">
                        <option value="300">
                        <option value="500">
                        <option value="800">
                        <option value="1000">
                        <option value="1500">
                    </datalist> 
                    <label class="span-3">代理费<span class="required">*</span></label>
                    <input class="text span-4" id="agency_fee" name="agency_fee"
                           value="<?php echo set_value('agency_fee', $trademark['agency_fee']); ?>" />
                    <label class="span-3">总费用</label>
                    <input class="text span-4" id="total_fee" name="total_fee" readonly="readonly"
                           value="<?php echo set_value('total_fee', $trademark['total_fee']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">是否收款</label>
                    <label class="span-4 left">
                        <?php echo form_checkbox('is_collected', '是', 
                                set_value('is_collected', $trademark['is_collected']) == '是',
                                'id="is_collected"'); ?>是
                    </label>
                    <label class="span-3" for="collection_date">收款日期</label>
                    <input class="text span-4 normal-date" id="collection_date" name="collection_date" readonly="readonly"
                           value="<?php echo set_value('collection_date', $trademark['collection_date']); ?>" />
                    <label class="span-3" for="collection_account">收款账户</label>
                    <input class="text span-4" id="collection_account" name="collection_account"
                           value="<?php echo set_value('collection_account', $trademark['collection_account']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">是否开票</label>
                    <label class="span-4 left">
                        <?php echo form_checkbox('is_billed', '是', 
                                set_value('is_billed', $trademark['is_billed']) == '是',
                                'id="is_billed"'); ?>是
                    </label>
                    <label class="span-3" for="billing_date">开票日期</label>
                    <input class="text span-4 normal-date" id="billing_date" name="billing_date" readonly="readonly"
                           value="<?php echo set_value('billing_date', $trademark['billing_date']); ?>" />
                    <label class="span-3" for="billing_amount">开票金额</label>
                    <input class="text span-4" id="billing_amount" name="billing_amount"
                           value="<?php echo set_value('billing_amount', $trademark['billing_amount']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4" for="drawer">开票人</label>
                    <input class="text span-4" id="drawer" name="drawer"
                           value="<?php echo set_value('drawer', $trademark['drawer']); ?>" />
                    
                    <label class="span-3">确认人</label>
                    <input class="text span-3" id="confirmor" name="confirmor" readonly="readonly"
                           value="<?php echo set_value('confirmor', $trademark['confirmor']); ?>" />
                    <button type="button" class="adhere" id="set_confirmor">...</button>
                    <?php echo form_dropdown('confirmor_id', $financial_users, '', 'class="span-4 hide" id="confirmor_id"'); ?>
                    <label class="span-3">官方扣款日期</label>
                    <input class="text span-4 normal-date" name="checkoff_date" readonly="readonly"
                           value="<?php echo set_value('checkoff_date', $trademark['checkoff_date']); ?>" />
                    <div class="clear"></div>
                </div>
            </div>
            <div id="tabs-3">
                <?php echo $dynamic_html; ?>
            </div>
            <div id="tabs-4">
                <?php if (isset($reminders_html)) echo $reminders_html; ?>
                <button type="button" id="add_reminder">新增</button>
            </div>
            <div id="tabs-5">
                <?php if (isset($documents_html)) echo $documents_html; ?>
                <button type="button" id="add_document">新增</button>
            </div>
        </div>
        <div class="form-actions">
            <input type="submit" class="form-submit" value="保存" />
        </div>
    </form>
</div>
<script>
    $(function() {
        $("#tabs").tabs();
        
        assignEvent($("body"));
        
        $(".chosen-select").chosen({
            disable_search_threshold: 10, // 少于10个就不显示搜索
            no_results_text: "Oops, nothing found!",
            inherit_select_classes: true,
            search_contains: true,
        });
        
        $("#ap_sample_name").yeFileUpload({
            url: "<?php echo site_url('upload/do_upload'); ?>",
            nameAttr: "id",
            maxSize: "200KB"
        });
        
        /* 案件类型 */
        $("#case_type").change(function() {
            $.getJSON( "<?php echo site_url('trademark/get_dynamic_info'); ?>", {"case_type" : this.value}, function( data ) {
                $("a[href='#tabs-3']").text(data.title);
                var element = $(data.html);
                assignEvent(element);
                
                $("#tabs-3").empty();
                $("#tabs-3").append(element);
            });
        });
        
        /* 申请人 */
        $("#c_id_chosen").removeClass("chosen-container").hide();
        $("#set_c_name").click(function() {
            $("#c_name").hide();
            $(this).hide();
            $("#c_id_chosen").addClass("chosen-container").show();
        });
        $("#c_id").change(function () {
            /* 载入申请人信息和联系人信息 */
            $.getJSON("<?php echo site_url('trademark/get_client') ?>", {"c_id": this.value},
            function (data) {
                var client = data.client;
                $("#c_name_en").val(client.c_name_en);
                $("#c_addr").val(client.c_addr);
                $("#c_addr_en").val(client.c_addr_en);
                $("#c_licence_number").val(client.c_licence_number);
                $("#c_country").val(client.c_country);
                $("#ct_mailing_addr").val(client.c_mailing_addr);
                
                var contact = data.contact;
                if (contact !== null) {
                    $("#ct_contact").val(contact.ct_contact);
                    $("#ct_telephone").val(contact.ct_telephone);
                    $("#ct_fax").val(contact.ct_fax);
                    $("#ct_qq").val(contact.ct_qq);
                }
            });

            $("#c_name").val($(this).find("option:selected").text());
            $("#c_id_chosen").removeClass("chosen-container").hide();
            $("#c_name").show();
            $("#set_c_name").show();
        });
        
        /* 代理人 */
        $("#set_agent").click(function() {
            $("#agent").hide();
            $(this).hide();
            $("#agent_id").show();
        });
        
        $("#agent_id").change(function() {
            $("#agent").val($(this).find("option:selected").text());
            $(this).val('');
            $(this).hide();
            $("#agent").show();
            $("#set_agent").show();
        });
        
        /* 合作所 */
        $("#set_partner").click(function() {
            $("#partner").hide();
            $(this).hide();
            $("#partner_id").show();
        });
        
        $("#partner_id").change(function() {
            /* 载入联系人信息 */
            $.getJSON("<?php echo site_url('trademark/get_partner') ?>", {"p_id": this.value},
            function (data) {
                var partner = data.partner;
                $("#ct_mailing_addr").val(partner.p_mailing_addr);
                
                var contact = data.contact;
                if (contact !== null) {
                    $("#ct_contact").val(contact.ct_contact);
                    $("#ct_telephone").val(contact.ct_telephone);
                    $("#ct_fax").val(contact.ct_fax);
                    $("#ct_qq").val(contact.ct_qq);
                }
            });

            $("#partner").val($(this).find("option:selected").text());
            $(this).val('');
            $(this).hide();
            $("#partner").show();
            $("#set_partner").show();
        });
        
        /* 改变官费和代理费时计算总费用 */
        $("#government_fee, #agency_fee").change(function() {
            var sum = parseFloat($("#government_fee").val()) + parseFloat($("#agency_fee").val());
            $("#total_fee").val(isNaN(sum) ? '' : sum);
        });
        
        /* 是否收款click */
        $("#is_collected").click(function() {
            if (this.checked) {
                $("label[for=collection_date]").append('<span class="required">*</span>');
                $("label[for=collection_account]").append('<span class="required">*</span>');
            } else {
                $("label[for=collection_date]").children('span').remove();
                $("label[for=collection_account]").children('span').remove();
                
                $("#collection_date").val('');
                $("#collection_account").val('');
            }
        });
        
        /* 是否开票click */
        $("#is_billed").click(function() {
            if (this.checked) {
                $("label[for=billing_date]").append('<span class="required">*</span>');
                $("label[for=billing_amount]").append('<span class="required">*</span>');
                $("label[for=drawer]").append('<span class="required">*</span>');
            } else {
                $("label[for=billing_date]").children('span').remove();
                $("label[for=billing_amount]").children('span').remove();
                $("label[for=drawer]").children('span').remove();
                
                $("#billing_date").val('');
                $("#billing_amount").val('');
                $("#drawer").val('');
            }
        });
        
        /* 确认人 */
        $("#set_confirmor").click(function() {
            $("#confirmor").hide();
            $(this).hide();
            $("#confirmor_id").show();
        });
        
        $("#confirmor_id").change(function() {
            $("#confirmor").val($(this).find("option:selected").text());
            $(this).val('');
            $(this).hide();
            $("#confirmor").show();
            $("#set_confirmor").show();
        });
        
        /* 限时提醒 */
        $("#add_reminder").click(function() {
            var element = $('<?php echo $empty_reminder_html; ?>');
            assignEvent(element);
            
            $(this).before(element);
        });
        
        /* 官方文件 */
        $("#add_document").click(function() {
            var element = $('<?php echo $empty_document_html; ?>');
            assignEvent(element);

            $(this).before(element);
        });
    });
    
    /**
     * 事件处理函数赋值
     * @param {type} container
     * @returns {undefined}     */
    function assignEvent(container) {
        $(container).find("button:not(.adhere)").button();
        $(container).find(".normal-date").datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: "yy-mm-dd",
        });
        $(container).find(".image-link").magnificPopup({type:'image'});

        /* 限时提醒 */
        $(container).find('button.remove-reminder').click(function() {
            $(this).parents('fieldset').remove();
        });
            
        /* 官方文件 */
        $(container).find("input[name^=doc_original_name]").yeFileUpload({
            url: "<?php echo site_url('upload/do_upload'); ?>",
            nameAttr: "this",
            pathAttr: "next",
            maxSize: "5MB"
        });
        $(container).find('button.remove-document').click(function() {
            $(this).parent().remove();
        });
            
    }
</script>